#!/bin/bash
# Program:
# 	Step 1 : Install Arch linux before Chroot
# 
# History:
# 	2014/04/12 Potter Version 1.0.0

# Test Is Root
# if [ "$UID" != "0" ]
if [ "$(id -u)" != "0" ]
then
	echo "Error: you cannot perform this operation unless you are root."
	exit
fi

# Log file
logfile=/mnt/BeforeChroot.log

# Confirm Partition
ToPartition=N
read -p "Are you sure to partition and mkfs ? It will delete all the partitions include your operating systems.(Y/N)" ToPartition
if [ "$ToPartition" != "Y" ]
then 
	echo "You have said No or Input error."
	exit
fi

# Device
sdx=sdxxxxxxxxxxxxxx
echo "Which device to Install Archlinux : "
echo "Such as: sda sdb sdc sdd ..."
echo -n "Choice: "
read sdx 
device_sdx=$sdx # To modify after chroot Script
sdx="/dev/"${sdx}
Device_exist=$(fdisk -l | grep -i $sdx | wc -l)

while [ $Device_exist -eq 0 -o ${#sdx} -lt 8 ] 
do
    echo "Input Error. Or too short..."
    echo "$sdx doesn't exist"
    echo "Try again..."
    echo "Which device to Install Archlinux : "
    echo "Such as: sda sdb sdc sdd ..."
    echo -n "Choice: "
    read sdx 
	device_sdx=$sdx
    sdx="/dev/"${sdx}
    Device_exist=$(fdisk -l | grep -i $sdx | wc -l)
done
echo "Found $sdx!"

# Umount
umount -R /mnt
umount $sdx*

partition_number=$(fdisk -l | grep -i $sdx | wc -l)
# Delete Old Partitions
delete_partition() {
fdisk $sdx << EOF
d

w
EOF
}

for ((i=1; i<$partition_number-1; i++)); do
	delete_partition
done
# the last
fdisk $sdx << EOF
d
w
EOF

# Create New Partitions
create_partition() {
fdisk $sdx << EOF
n
p
1

+4G
n
p
2

+100G
n
p
3

+100M
n
p


w
EOF
}

create_partition
# Test if /dev/sda1 /dev/sda2 /dev/sda3 /dev/sda4
#if [ ! -b $sdx"1" ] || [ ! -b $sdx"2" ] || [ ! -b $sdx"3" ] || [ ! -b $sdx"4" ]
#then
#	echo "Error, Please Partation"
#	echo "windows read  --> ${sdx}1"
#	echo "/    			--> ${sdx}2"
#	echo "boot 			--> ${sdx}3"
#	echo "home 			--> ${sdx}4"
#	exit
#fi

# 1.mkfs
mkfs.vfat ${sdx}"1" # windows
mkfs.ext4 ${sdx}"2" # / 100G
mkfs.ext4 ${sdx}"3" # /boot 100M
mkfs.ext4 ${sdx}"4" # /home

# 2.mount / /boot /home
mount ${sdx}"2" /mnt # /
mkdir /mnt/boot
mount ${sdx}"3" /mnt/boot # boot
mkdir /mnt/home
mount ${sdx}"4" /mnt/home # home

# Partition Table Infomation
echo "swap 			--> ${sdx}1" >> $logfile
echo "/    			--> ${sdx}2" >> $logfile
echo "boot 			--> ${sdx}3" >> $logfile
echo "home 			--> ${sdx}4" >> $logfile
fdisk -l | grep -i $sdx >> $logfile
echo "" >> $logfile

# pacstrap genfstab arch-chroot
cd /usr/bin
wget -O pacstrap http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/pacstrap && chmod +x pacstrap && \
wget -O genfstab http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/genfstab && chmod +x genfstab && \
wget -O arch-chroot http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/arch-chroot && chmod +x arch-chroot && \
cd ~ && \

# 3.vim /etc/pacman.d/mirrorlist
if [ ! -f "/etc/pacman.d/mirrorlist.bac" ]
then
	cd /etc/pacman.d && \
	cp mirrorlist mirrorlist.bac
fi
wget -O /etc/pacman.d/mirrorlist http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/mirrorlist && \

pacman -Syy &&\

# 4.base system
pacstrap /mnt base base-devel && \

# 5.genfstab
genfstab -p -U /mnt >> /mnt/etc/fstab && \

# 6.After Chroot , Install Packages Script
cd /mnt && \
wget -O 2.sh http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/UDisk/2.after_chroot_for_flashdisk.sh && \
# 6.1 modify the Device installed Grub 
sed -i "s/sdx/$device_sdx/g" 2.sh && \
cp 2.sh /mnt/usr/bin/2.sh && \
chmod +x /mnt/usr/bin/2.sh && \

# 7.chroot
# arch-chroot /mnt /bin/bash
arch-chroot /mnt /usr/bin/2.sh

# out of chroot
umount -R /mnt

